package com.atguigu.tingshu.account.mapper;

import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;

@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccount> {

    @Update("    update  user_account set available_amount = available_amount -#{amount},\n" +
            "         total_pay_amount = total_pay_amount +#{amount},\n" +
            "         total_amount = total_amount -#{amount}\n" +
            "    where user_id =#{userId} and available_amount>=#{amount} and is_deleted =0 ")
    int checkAndDeduct(@Param("amount") BigDecimal amount, @Param("userId") Long userId);


    @Update("update user_account set total_amount = total_amount+#{rechargeAmount},available_amount=available_amount+#{rechargeAmount},\n" +
            "         total_income_amount = total_income_amount + #{rechargeAmount} where user_id =#{userId} and is_deleted =0")
    int updateAvailableAmount(@Param("userId")Long userId,@Param("rechargeAmount") BigDecimal rechargeAmount);
}
